package com.jiazhong.mingxing.boot.boot11.批量消息;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class 批量消息生产 {

    @SneakyThrows
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("jiazhong-e");
        producer.setNamesrvAddr("124.220.170.254:9876");
        producer.start();
        List<Message> messages = new ArrayList<>();
        for (int i = 1; i < 21; i++) {
            String topic = "TopicD";
            String tags = "piliang";
            String body = "Hello,PiLiang!Index is " + i;
            Message message = new Message(topic, tags, body.getBytes());
            messages.add(message);
        }
        // 一次发送20条消息（就发送了一次）
        SendResult sendResult = producer.send(messages);
        log.info("sendResult={}", sendResult);
        producer.shutdown();
    }
}
